/*

Author   : chaos

FileName : ptr_fun2.cpp

Date     : 10/08/17 01:03:33

*/
#include <iostream>
using namespace std;

void bubble(int *,int,int(*)(int, int));
void prn_arr(int *,int);

int ascend(int a, int b){return a>b;}
int descend(int a, int b){return a<b;}

int main(int argc ,char *argv[])
{

    int array[5] = {3,5,2,4,6};

    cout << "Origin arr =";
    prn_arr(array,5);
    cout << endl;

    bubble(array,5,ascend);

    cout << "ascenging arr =";
    prn_arr(array,5);
    cout << endl;

    bubble(array,5,descend);

    cout << "descending arr=";
    prn_arr(array,5);
        cout << endl;

    bubble(array,5,ascend);
    return 0;
}


void bubble(int *arr,int size, int (*compare)(int,int))
{
    int i,j,temp;
    for(i=1;i<(size-1);i++)
        for(j=0;j<(size - i);j++)
            if(compare(arr[j],arr[j+1]))
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1]=temp;
            }
}

void prn_arr(int *arr, int size)
{
    int i;

    for(i=0;i<size;i++)
        cout << " " << arr[i];

}
